layui.use(['form','laypage','layer'],function(){
	
	var form = layui.form,layer = layui.layer,laypage = layui.laypage;
	
	$("#param").on('keydown',function(e){
		if(e.keyCode==13){
			initPage();
		}
	});
	
	  initPage();
	  
	  
	  function getData(pageIndex,pageSize){
	 	  var data = {};
	 	  data["pageIndex"] = pageIndex?pageIndex:1;
	 	  data["pageSize"] = pageSize?pageSize:10;
	 	  var serialNumber = $("#serialNumber").val();
	 	  data["serialNumber"] = serialNumber;
	 	  var brand = $("#brand").val();
	 	  data["brand"] = brand;
	 	  return data;
	   	}
	  
	  
	  function initPage(){
		  var data = getData();
		  ajaxPost("../device/laylist",data,
				  function(data){
			  createData(data.data.list);
			  laypage.render({
				  elem:'page',
				  count:data.data.total,
				  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
			    ,jump: function(obj,first){
			    	if(!first){
			    		toPage(obj.curr,obj.limit);
			    	}
			    }
			  });
		  },
				  function(data){
					  layer.alert(data.errorMsg,{
							icon:5,
							title:"错误"
						});
				  });
	  }
	  
	  window.initPage = initPage;
	  function toPage(pageIndex,pageSize){
	 	  
	 	var data = getData(pageIndex,pageSize);
	 	  ajaxPost("../device/laylist",data,
	 			  function(data){createData(data.data.list);},
	 			  function(data){
	 				  layer.alert(data.errorMsg,{
	 						icon:5,
	 						title:"错误"
	 					});
	 			  });
	   }
	  
	  
	  function createData(data){
		  var deviceIds = $("#deviceIds").val();
		  if(isBlank(deviceIds))
			  deviceIds = "";
		  var selIds = deviceIds.split(",");
		  var content = [];
		  for(var i =0 ;i < data.length;i++){
			  var devicedata = data[i];
			  var id = devicedata.id;
			  content.push('<tr>');
			  content.push('<td>'+devicedata.serialNumber+'</td>');
			  content.push('<td>'+devicedata.platform+'</td>');
			  content.push('<td>'+devicedata.brand+'</td>');
			  content.push('<td>'+devicedata.model+'</td>');
			  content.push('<td> <button  class="layui-btn" deviceId="'+id+'"  type="button" onclick="showcase(this)">演示</button></td>')
			  content.push('</tr>');
		  }
		  
		  $("#dg").empty().append(content.join(""));
	  }
	  var socket;
	  var webSocketPort = null;
	  var webSocketIP = null;
	  function showcase(obj){
		  var text = $(obj).text();
		  var deviceId =  $(obj).attr("deviceId");
		  var data1 = {};
	 	  
		  if(text == "演示"){
			  
			 var index =  layer.confirm('是否安装APP？', {
				  btn: ['安装','不安装'] //按钮
				}, function(){
				   //安装的逻辑在这里
					data1["reuse"]=2;
					data1["deviceId"] = deviceId;
				 	  data1["caseId"] = $("#caseId").val();
					  ajaxPost("../device/showDevice",data1,
				 			  function(data){
						  		if(data.status==1){
						  			 webSocketIP=data.webSocketIP;
									  webSocketPort=data.webSocketPort;
									  showcaseByDevice();
									  $(obj).text("结束");
						  		}else{
						  			layer.alert(data.errorMsg,{
				 						icon:5,
				 						title:"错误"
				 					});
						  		}
								 
					  			},
				 			  function(data){
				 				  layer.alert(data.errorMsg,{
				 						icon:5,
				 						title:"错误"
				 					});
				 			  });
					layer.close(index);
				}, function(){
					//不安装的逻辑在这里
					data1["reuse"]=3;
					data1["deviceId"] = deviceId;
				 	data1["caseId"] = $("#caseId").val();
					  ajaxPost("../device/showDevice",data1,
				 			  function(data){
								  webSocketIP=data.webSocketIP;
								  webSocketPort=data.webSocketPort;
								  showcaseByDevice();
								  $(obj).text("结束");
					  			},
				 			  function(data){
				 				  layer.alert(data.errorMsg,{
				 						icon:5,
				 						title:"错误"
				 					});
				 			  });
				});
			  
			  
			  
			  
		  }else{
			  data1["deviceId"] = deviceId;
		 	  //data1["caseId"] = $("#caseId").val();
			  ajaxPost("../device/givebackDevice",data1,
		 			  function(data){
				  		socket.close();
				  		$('#screenshot').attr('src', '');
			  			},
		 			  function(data){
		 				  layer.alert(data.errorMsg,{
		 						icon:5,
		 						title:"错误"
		 					});
		 			  });
			  $(obj).text("演示");
		  }
		  
		  
	  }
	  window.showcase = showcase;
	  function showcaseByDevice(){
		  socket = new WebSocket('ws://'+webSocketIP+':'+webSocketPort);
		  var screenshot = document.getElementById('screenshot');
		// 打开Socket
          socket.onopen = function (event) {

              // 监听消息
              socket.onmessage = function (event) {
                  var blob = new Blob([event.data], {type: 'image/jpeg'});
                  var u = URL.createObjectURL(blob);
                  $('#screenshot').attr('src', u);
              };

              // 监听Socket的关闭
              socket.onclose = function (event) {
                  console.log('Client notified socket has closed', event);
              };
          };
	  }
	 
});